// pages/company/details/details.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    itemDetail: "",
    volume: '',
    volumeWeight: '',
    isCal: false,
    tip: '',
    id: '',
    isShop:false,//下游承运商
    firstshop:'',
    isshops:false,//承运商下拉
    partnerNames:[],//承运商列表
    styles:[{style:'上门提货',id:1},{style:'客户送货',id:2}],
    isstyles:false,
    firststyle:'上门提货',
    region: ['浙江省', '宁波市', '海曙区'],
    inptext:'',
    inpname:'',
    inpphone:'',
    inprake:'',
    code:'',
    pid:'',
    sid:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({
      id: options.id
    })
    var company = wx.getStorageSync("COMPANY");
    app.getApisj.request(
      "/api/Orders/Detail?id=" + options.id, {
      },
      "GET",
      res => {
        wx.hideLoading();
        if (res.data.responseCode == 1000) {
          //列表数据
          var orderGoodInfosList = res.data.obj.orderGoodInfosList
          var volume = 0, volumeWeight = 0
          if (orderGoodInfosList.length) {
            for (var i = 0; i < orderGoodInfosList.length; i++) {
              volume += orderGoodInfosList[i].volume
              volumeWeight += orderGoodInfosList[i].volumeWeight
            }
          }
          console.log(res.data.obj)
          that.setData({
            itemDetail: res.data.obj,
            volume: Number(volume),
            volumeWeight: Number(volumeWeight)
          });

        } else {
          wx.showToast({
            title: '请求失败',
            icon: 'loading',
            duration: 2000
          });
        }

      },
      res => { }
    );


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */

  yes() {//转运
    var that = this
    if (that.data.inptext == '') {
      wx.showToast({
        title: '请输入地址',
        icon: 'loading',
        duration: 1500
      });
      return false
    }
    if (that.data.inpname == '') {
      wx.showToast({
        title: '请输入名字',
        icon: 'loading',
        duration: 1500
      });
      return false
    }
    if (that.data.inpphone == '') {
      wx.showToast({
        title: '请输入电话',
        icon: 'loading',
        duration: 1500
      });
      return false
    }
    that.setData({
      status: 2,
      isCal: true,
      tip: "是否确认转运"
    })
  },
  cal() {//取消
    var that = this
    that.setData({
      isCal: false,
    })
  },

  shop(){//选择承运商
  
  var that = this
  that.setData({
    isShop:true
  })
    var company = wx.getStorageSync("COMPANY");
    app.getApisj.request(
      "/api/Partners/DownstreamCarrierForSelect", {
        company: {
          id: company.id,
          companyName: company.companyName,
        },
      },
      "POST",
      res => {
        wx.hideLoading();
        if (res.data.responseCode == 1000) {
          //列表数据
          console.log(res.data.obj)


          that.setData({
            firstshop: res.data.obj[0].partnerName,
            code: res.data.obj[0].partnerCode,
            pid: res.data.obj[0].id,
            partnerNames: res.data.obj
          });

        } else {
          wx.showToast({
            title: '请求失败',
            icon: 'loading',
            duration: 2000
          });
        }

      },
      res => { }
    );    
  },
  contgoods(){//选择承运商
      var that=this
      that.setData({
        isshops: !that.data.isshops
      })
  },
  itemshop(e){//点击选择承运商
    var that = this
    that.setData({
      firstshop: e.currentTarget.dataset.name,
      code: e.currentTarget.dataset.code,
      pid: e.currentTarget.dataset.pid
    })    
  },
  contstyles(){//选择交接方式
    var that = this
    that.setData({
      isstyles: !that.data.isstyles
    })   
  },
  itemstyle(e){//
    var that = this
    that.setData({
      firststyle: e.currentTarget.dataset.name,
      sid: e.currentTarget.dataset.sid
    })   
  },



  bindRegionChange(e){//地址
      this.setData({
        region: e.detail.value,
      })
  },
  inputtxt(e){//详细地址
      this.setData({
        inptext: e.detail.value,
      })
  },
  inputname(e) {//名字
    this.setData({
      inpname: e.detail.value,
    })
  },
  inputphone(e) {//电话
    this.setData({
      inpphone: e.detail.value,
    })
  },
  inputrake(e) {//备注
    this.setData({
      inprake: e.detail.value,
    })
  },

  subt(){//承运商显示隐藏
      var that=this
      that.setData({
        isShop: false
      })
  },

  /**
   * 生命周期函数--监听页面显示
   */

  sub(){//提交转运
    var company = wx.getStorageSync("COMPANY");
    var tokenL = app.globalData.token
    console.log(tokenL,"55555555")
    var that = this
    var itemDetail = that.data.itemDetail
    wx.request({
      url: app.globalData.apiServer + "/api/OrdersTransports/Trans",
      data: {
        company: {
          id: company.id,
          companyName: company.companyName,
        },
        isFromOrder: true,
        shippingType: itemDetail.shippingType,
        partners: {
          id: that.data.pid,
          partnerCode: that.data.code,
          partnerName: that.data.firstshop
        },
        transitHandover: that.data.sid,
        addr: that.data.region[0] + that.data.region[1] + that.data.region[2] + that.data.inptext,
        contact: that.data.inpname,
        tel: that.data.inpphone,
        remark: that.data.inprake,
        ordersList: [
          {
            id: itemDetail.id,
          }
        ]
      },
      method:"POST",
      header: {
        'content-type': 'application/json',
        "Authorization": "Bearer " + tokenL,
        "LoginType": 4
      },
      success: function (res) {
        console.log(res.data,"----------")
        if (res.data.responseCode == 1000) {
          that.setData({
            isCal: false,
          })
          wx.showToast({
            title: '转运成功',
            icon: 'loading',
            duration: 2000
          }); 
          setTimeout(() => {
            wx.switchTab({
              url: '../index/index',
            })
          }, 2000)
           
        }else{
          wx.showToast({
            title: '请求失败',
            icon: 'loading',
            duration: 2000
          });         
        }
    
      }

    })

  }
})